using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Graphics;
using WPCoco.Helpers;
using Microsoft.Xna.Framework;

namespace WPCoco.Tilesets.Codecs
{
    public class PlistV3FormatReader : ITilesetReader
    {
        #region ITilesetReader Members

        public bool CanRead(XDocument document)
        {
            XElement plist = document.Root;
            if (plist.Name != "plist") return false;
            XElement dicte = plist.FirstNode as XElement;
            if (dicte == null) return false;
            IDictionary<string, object> dict = (IDictionary<string, object>)PlistHelper.XmlToElememt(document.Root.FirstNode as XElement);
            if (dict.Keys.Count != 2 || !dict.ContainsKey("metadata") || !dict.ContainsKey("frames")) return false;
            IDictionary<string, object> meta = (IDictionary<string, object>)dict["metadata"];
            if (!meta.ContainsKey("format") || meta["format"] == null) return false;
            int format = (int)meta["format"];
            if (format != 3) return false;
            return true;
        }

        Rectangle ParseRectangle(string str) 
        {
            Rectangle rect = new Rectangle();
            str = str.Replace("{", "");
            str = str.Replace("}", "");
            string[] chunks = str.Split(',');
            rect.X = int.Parse(chunks[0]);
            rect.Y = int.Parse(chunks[1]);
            rect.Width = int.Parse(chunks[2]);
            rect.Height = int.Parse(chunks[3]);
            return rect;
        }

        Point ParsePoint(string str)
        {
            Point p = new Point();
            str = str.Replace("{", "");
            str = str.Replace("}", "");
            string[] chunks = str.Split(',');
            p.X = int.Parse(chunks[0]);
            p.Y = int.Parse(chunks[1]);
            return p;
        }

        public Tileset Read(XDocument document, Texture2D texture)
        {
            IDictionary<string, object> dict = (IDictionary<string, object>)PlistHelper.XmlToElememt(document.Root.FirstNode as XElement);
            Tileset tileset = new Tileset();
            IDictionary<string, object> frames = (IDictionary<string, object>)dict["frames"];
            IDictionary<string, object> item;
            TextureFrame frame;
            Point offset, size;
            foreach (var key in frames.Keys)
            {
                item = (IDictionary<string, object>)frames[key];
                frame = new TextureFrame();
                frame.TextureRegion = ParseRectangle((string)item["textureRect"]);
                offset = ParsePoint((string)item["spriteOffset"]);
                size = ParsePoint((string)item["spriteSourceSize"]);
                frame.Texture = texture;
                frame.TextureTrim = new Trim(offset.X,
                                             offset.Y,
                                             size.X - offset.X - frame.TextureRegion.Width,
                                             size.Y - offset.Y - frame.TextureRegion.Height);
                tileset.Add(frame);
                tileset.AddAlias(key, tileset.Count - 1);
            }
            return tileset;
        }

        #endregion
    }
}
